
set more off

qui{


 global X "c.age##c.age  i.t ad_impw_atotb i.mstat  educy i.lbrf ragender i.raracem finr  i.cendiv "
global X2 "c.age##c.age  i.t ad_impw_atotb i.mstat  hs college i.lbrf ragender i.raracem i.cendiv i.first_loss_year" 


******************************************************************
*******Fig1: Age profile of self-reported and assessed memory*****
******************************************************************

set printcolor gs1

use wsample1, clear
keep if lfinr==1

 twoway (kdensity tot_rec   [fw=wtresp] ,   bwidth(2) lcolor( sand ) fcolor(sand)) ///
(kdensity dtot_rec  [fw=wtresp] ,   bwidth(2)  lcolor( black )  fcolor(none)), ///
 xtitle("Memory score") graphregion(fcolor(white)) legend (pos (6)  row(1) label( 1 "Levels") label( 2 "First differences")) ytitle("") 
graph export Fig1.eps, replace



 ******************************************************************
 *******Fig2: Age profile of self-reported and assessed memory*****
 ******************************************************************

   preserve
  /* xtreg tot_rec, fe
   predict r_tot_rec, e
    xtreg srmem, fe
   predict r_srmem, e
    xtreg vgood, fe
   predict r_vgood, e
  */ 
 egen srec=std(tot_rec) 
egen ssrmem=std(srmem) 
egen svgood=std(vgood) 



collapse  srec ssrmem svgood if fpstme==0 , by(ager)

sort age
 gen Recall=(srec+srec[_n-1]+srec[_n+1])/3
 gen sSRM=(ssrmem+ssrmem[_n-1]+ssrmem[_n+1])/3
 gen sSRMG=(svgood+svgood[_n-1]+svgood[_n+1])/3
rename ager Age
*gen Self_rated_Memory=(sr_mem+sr_mem[_n-1]+sr_mem[_n+1])/3


twoway   (line Recall Age , lc(sand)) (line sSRM Age , lc(black)) (line sSRMG Age , lc(gray*.8))  if Age>49 & Age<81 , legend (pos(6) label(1 "Memory score") label( 2 "Self-rated memory") label( 3 "Good self-rated memory") rows(1) size(small))   graphregion(fcolor(white))
 graph export Fig2.eps, replace
restore

******************************************************************
********Figure 3**************************************************
******************************************************************


use HRS_before_trim, clear
keep if age<20 & age>=-10
bysort hhidpn (t): egen first_r_loss = ifirst(r_loss) ,v(1) // 1 if first occurrence of r_loss

gen wave_loss = year if r_loss==1 
by hhidpn: egen first_loss_year = min(wave_loss)
gen time = (year - first_loss_year)/2

gen temp=age if first_r_loss==1
bysort hhidpn(t): egen age_loss=max(temp)
drop temp

*unaware based on the first episode
g temp=(first_r_loss == 1 & r_loss_u==1)
bys hhidpn: egen sample_u = max(temp)
drop temp


preserve
collapse age_loss sample_u  if wave_loss!=., by(hhidpn)
replace age_loss=age_loss+60
sum age_loss
sum age_loss if sample_u==1 //& age_loss>49 & age_loss<=80
	local	u_mean=r(mean) 
sum age_loss if sample_u==0 //& age_loss>49 & age_loss<=80
	local	a_mean=r(mean)
twoway (kdensity age_loss if sample_u==0 ,   bwidth(1.5)  lcolor( black )  fcolor(none)) ///
  (kdensity age_loss if sample_u==1   , bwidth(1.5)   lcolor(sand)) if age_loss>49 & age_loss<80, /// 
    xline(`a_mean', lcolor(black) lpattern(dash))  xline(`u_mean', lcolor(sand)  lpattern(dash)) ///
 xtitle("Age at first memory loss") graphregion(fcolor(white)) legend (pos(6) label( 1 "Aware") label( 2 "Unaware") rows(1))  ytitle("") xlabel(50(5)80)
graph export Fig3.eps, replace
restore

******************************************************************
********Figure 4**************************************************
******************************************************************

 use wsample2,clear 
 
 ******Robustness check to remove peopel with Hospitalization*********
*bysort hhidpn: egen temp=total(hosp)
*gen ev_hosp= temp>0
*drop temp 

*gen hos0=hosp==1 & time==0
*bysort hhidpn: egen temp=total(hos0)
*gen h0= temp>0
*drop temp 

* drop if hosp==1 //  | h_shock==1
*********************************


g t0 = first_r_loss == 1 & sample_l==1

forval n=1/9 {
	gen lead`n'= time==-`n'
	gen lag`n'= time==`n'
}
forval n=1/9 {
	gen leadcum`n'= time<=-`n'
	gen lagcum`n'=time>=`n'
}

 
 foreach y of varlist  leadcum2 lead2 lead1 t0 lag1 lag2 lagcum1 lagcum2  {
gen `y'_u=`y'*sample_u
gen `y'_a=`y'*sample_a
gen `y'_l=`y'*sample_l
}



 
global treatment2 "lead2_u t0_u lag1_u lag2_u sample_u"
 
 
		local letter b f
		foreach l of local  letter {

			if 	"`l'"=="b" {
			local w Total
			local p A

		}
		
			else {
			local w Financial
			local p C

		}
		
	xi: reg h_atot`l'c     $treatment2 $X2   lh_atot`l' lqrec  if  sample_a==0       [pw=wtresp] , r  cl(hhid) 
 foreach var of varlist $treatment2 {
scalar `var'_b=_b[`var']
scalar `var'_se=_se[`var']
	}
 
 
 preserve
 clear
 set obs 5
 gen t=(_n-3)*2
 local list "u"
 foreach g of local list {
	gen b_`g'=0 if t==-2
	replace b_`g'=lead2_`g'_b if t==-4
	replace b_`g'=t0_`g'_b if t==0
	replace b_`g'=lag1_`g'_b if t==2
		replace b_`g'=lag2_`g'_b if t==4

	gen  se_`g'=lead2_`g'_se if t==-4
	replace se_`g'=t0_`g'_se if t==0
	replace se_`g'=lag1_`g'_se if t==2
			replace se_`g'=lag2_`g'_se if t==4

	gen u_`g'=b_`g'+1.96*se_`g'
	gen l_`g'=b_`g'-1.96*se_`g'
	}
	
	
twoway (scatter b_u t, color (black))  (rcap l_u u_u t, color (black)),yline(0, lcolor(red)) xline(-1,  lcolor(red) lpattern(dash)) graphregion(fcolor(white)) legend (off) xtitle("Years relative to cognitive loss", `xtitle_options') title("`p'. `w' wealth: Unaware ") 
graph export Fig4_`p'.eps, replace
restore
		}


		
 *******aware as control group*******
 
 	local letter b f
		foreach l of local  letter {

			if 	"`l'"=="b" {
			local w Total
			local p B

		}
		
			else {
			local w Financial
			local p D

		}

 global treatment3 " lead2_u t0_u lag1_u lag2_u lead2_l  t0_l lag1_l lag2_l sample_u sample_l"

			 
	xi: reg h_atot`l'c     $treatment3 $X2   lh_atot`l' lqrec  if  sample_l==1        [pw=wtresp] , r  cl(hhid) 
 foreach var of varlist $treatment2 {
scalar `var'_b=_b[`var']
scalar `var'_se=_se[`var']
	}
 
 
 preserve
 clear
 set obs 5
 gen t=(_n-3)*2
 local list "u"
 foreach g of local list {
	gen b_`g'=0 if t==-2
	replace b_`g'=lead2_`g'_b if t==-4
	replace b_`g'=t0_`g'_b if t==0
	replace b_`g'=lag1_`g'_b if t==2
		replace b_`g'=lag2_`g'_b if t==4

	gen  se_`g'=lead2_`g'_se if t==-4
	replace se_`g'=t0_`g'_se if t==0
	replace se_`g'=lag1_`g'_se if t==2
			replace se_`g'=lag2_`g'_se if t==4

	gen u_`g'=b_`g'+1.96*se_`g'
	gen l_`g'=b_`g'-1.96*se_`g'
	}
	
	
twoway (scatter b_u t, color (black))  (rcap l_u u_u t, color (black)),yline(0, lcolor(red)) xline(-1,  lcolor(red) lpattern(dash)) graphregion(fcolor(white)) legend (off) xtitle("Years relative to cognitive loss", `xtitle_options') title("`p'. `w' wealth: Unaware vs. aware")  // ylabel(-250(50)250)
graph export Fig4_`p'.eps, replace

	restore	
		}

set printcolor asis


 ********************************************************************
****Figure A.1: Financial market returns by asset category, 1998–2014 
********************************************************************
  
do "5_1_stock_market_returns.do"

  
*******************************************************
*******FigB1 Relation with other cognitive measures****
*******************************************************

use wsample1, clear

foreach y of varlist ser7 bwc20 mstot vocab numeracy {
estpost tab `y'
   estout .,cells("b(fmt(%9.0f)) pct(fmt(%9.3f)) cumpct(fmt(%9.3f))") style(tex) 
}

  qui reg st_ser7 r_loss decline_nloss $X     if  fser7==0  [pw=wtresp] , r cluster (hhid)
  gen sample=1 if e(sample)==1

gen numer3=numeracy==3
replace numer3=. if numeracy==.


 gen loss3=1 if r_loss==0
 replace loss3=2 if r_loss_a==1
 replace loss3=3 if r_loss_u==1


 preserve
 xtset hhidpn t
 collapse (mean) C3=D.st_mstot C2=D.st_bwc C1=D.st_ser7  (sd) S3=D.st_mstot S2=D.st_bwc  S1=st_ser7 (count) n3=st_mstot n2=st_bwc  n1=D.st_ser7 ///
 if sample==1  [weight=wtresp],by (loss3)
  
reshape long C S n, i (loss3) j(test)

  generate high_C = C + invttail(n-1,0.05)*(S / sqrt(n))
  generate low_C = C - invttail(n-1,0.05)*(S/ sqrt(n))

  generate loss_C = loss3-.5    if test == 1
replace  loss_C = loss3 +3  if test == 2
replace  loss_C = loss3 +7  if test == 3

  twoway (bar C loss_C if loss3==1,  fcolor(black)) (bar C loss_C if loss3==2,  fcolor(gray*.3)) (bar C loss_C if loss3==3,  fcolor(sand)) (rcap high_C low_C loss_C), ///
legend(order(1 "No severe memory loss" 2 "Aware" 3 "Unaware") pos(6) rows(1)) graphregion(fcolor(white)) ///
    xlabel( 1 "{&Delta}Serial 7" 5 "{&Delta}Backward counting" 9 "{&Delta}Mental status", noticks) xtitle("")  ytitle (Standard deviations)
	graph export FigB1.eps, replace
restore
 
******************************************************************
 ***********Fig B2 Previous memory, Loss vs. no loss****************
******************************************************************
 
 *with the absolute definition such differences are even larger
twoway (kdensity ltot_r if r_loss==0  [fw=wtresp] ,   bwidth(2)  lcolor( black )  fcolor(none)) ///
(kdensity ltot_r if r_loss_a==1  [fw=wtresp] ,  bwidth(2)   lcolor( gs10 )  fcolor(none)) ///
  (kdensity ltot_r if r_loss_u==1  [fw=wtresp] , bwidth(2)   lcolor(sand)), ///
 xtitle("Memory score in previous wave") graphregion( fcolor(white)) legend (label(1 "No severe memory loss") label( 2 "Aware") label( 3 "Unaware") pos (6) rows(1))  ytitle("")
graph export FigB2.eps, replace
 
 
 
 
******************************************************************
**Figure B.3**********Sample size event study by treatment status*********
******************************************************************

use ws2_full, clear 
preserve
*table time treatment  
*noi  tabout time treatment  using ev_t.tex, replace style(tex) format(0) 
  

replace time=. if control==1
collapse (count) N=h_atotbc , by(treatment time)
save temp, replace
restore 

preserve
replace time=. if control==1
collapse (count) N=h_atotbc , by(time)
gen treatment=3
append using temp

twoway (line N time if treatment==1,  lcolor( gray )) (line N time if treatment==2,  lcolor( sand ) ) (line N time if treatment==3,  lcolor( black ) ) if time<8 & time>=-7, graphregion(fcolor(white)) legend (label( 1 "Aware") label( 2 "Unaware") label( 3 "Total") rows(1) pos (6))  xtitle("Event time") ytitle("") xlabel(-7(1)7) // xline(-2, lcolor(red))  xline(2, lcolor(red))
graph export FigB3.eps, replace
restore

******************************************************************
**FigureB.4:time-heterogeneity, regression interacted with years*
******************************************************************

   use wsample1, clear
   global X "$X0 i.mstat  educy i.lbrf ragender i.raracem finr  i.cendiv ad_impw_atotb"

	   
	   keep if lfinr==1
	   forvalue y=2000(2)2014 {
	   	gen a_`y'=(r_loss_a==1& year==`y')
	   	gen u_`y'=(r_loss_u==1& year==`y')

		} 
	   
	   
	  rename a_2* y_2*
	   reg h_atotbc r_decline_n y_2* u_2*  lqrec lh_atotb $X        [pw=wtresp] , r cluster (hhid)
	   est store Aware
	    rename y_2*  a_2*
	  rename u_2* y_2*		
	   reg h_atotbc r_decline_n y_2* a_2*  lqrec lh_atotb $X     [pw=wtresp] , r cluster (hhid)
	   est store Unaware
	   
	   forvalue n=2000(2)2014 {
	   label var y_`n' "`n'"
	   }
	   coefplot  Aware Unaware  ,  vertical  yline(0) keep(y_2*) graphregion(fcolor(white)) xtitle(Year)  legend (label( 2 "Aware") label( 1 "Unaware") rows(1) pos (6)) 
	   graph export FigB4.eps, replace
****
erase temp.dta
}
